package choosecity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;

import base.BaseActivity;
import choosecity.db.DBManager;
import util.utls.Const;
import util.utls.SPUtils;

/**
 * 搜索城市
 */
public class ChooseCityActivity extends BaseActivity {
    ImageView back;
    TextView tvTitle;
    EditText edit_inputcity;
    ArrayList<String> arrayStrings = new ArrayList<String>();
    private SQLiteDatabase database;
    protected Cursor cursor;
    ListView listview;
    ChooseCityListAdapter adapter;
    private DBManager dbManager;
    ImageView delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_city);
        setStatusBarColor(R.color.blue);
        tvTitle.setText(R.string.sousuo_cirt);
        adapter = new ChooseCityListAdapter(this, arrayStrings);
        listview.setAdapter(adapter);

        dbManager = new DBManager(this);
        dbManager.openDateBase();
        database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/"
                + DBManager.DB_NAME, null);
        delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                edit_inputcity.setText("");
            }
        });
        edit_inputcity.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before,
                                      int count) {
                // TODO Auto-generated method stub
                if(s.length()>0){
                    delete.setVisibility(View.VISIBLE);
                }else
                {
                    delete.setVisibility(View.GONE);
                }
            //    arrayStrings = new ArrayList<String>();
                // 创建模糊查询的条件
                String content = edit_inputcity.getText().toString();
                if (!TextUtils.isEmpty(content)) {
                    dbManager = new DBManager(ChooseCityActivity.this);
                    String likeStr = "'";
                    for (int i = 0; i < content.length(); i++) {
                        if (i < content.length() - 1) {
                            likeStr += "%" + content.charAt(i);
                        } else {
                            likeStr += "%" + content.charAt(i) + "%'";
                        }
                    }
                    cursor = database.rawQuery(("select * from " + "T_city"
                            + " where CityName like " + likeStr), null);
                    arrayStrings = queryInfo(cursor);
                    adapter.refreshData(arrayStrings);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                                          int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                // TODO Auto-generated method stub
                edit_inputcity.setText(arrayStrings.get(position));
                Intent data=new Intent();
                if(arrayStrings.size()>0){
                    data.putExtra("choose", arrayStrings.get(position));
                }

                setResult(102,data);
                finish();
                SPUtils.put(ChooseCityActivity.this, null,
                        Const.CITY, arrayStrings.get(position));
                Intent intent=new Intent("city");

                sendBroadcast(intent);
            }
        });
    }
    public ArrayList<String> queryInfo(Cursor cursor) {
        try {
            while (cursor.moveToNext()) {
                arrayStrings.add(cursor.getString(cursor
                        .getColumnIndex("CityName")));
            }

        } catch (Exception e) {
            // TODO: handle exception
            e.toString();
        } finally {
            // database.close();
        }

        return arrayStrings;
    }
    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
       switch (v.getId()){
           case R.id.back:
               finish();
           break;
       }
    }
}

